<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\InvestmentEnvironment;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class InvestmentEnvironmentController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new InvestmentEnvironment(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('title');
            $grid->column('cover', '封面图')->image('', 120, 60);
            $grid->column('description');
            $grid->column('tag')->explode()->label();
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new InvestmentEnvironment(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('cover', '封面图')->image();
            $show->field('description');
            $show->field('content')->unescape();
            $show->field('tag')->explode()->label();
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new InvestmentEnvironment(), function (Form $form) {
            $form->display('id');
            $form->text('title');
            $form->image('cover', '封面图')->autoUpload()->saveFullUrl();
            $form->textarea('description');
            $form->editor('content');
            $form->tags('tag')->help('标签输入完成后要以逗号结尾')
                ->saving(function ($v) {
                    return implode(',', $v);
                });

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
